package com.shuqing.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shuqing.dao.BookMapper;
import com.shuqing.domain.Book;
import com.shuqing.domain.Record;
import com.shuqing.domain.User;
import com.shuqing.service.BookService;
import com.shuqing.service.RecordService;
import com.shuqing.utils.PageResult;
import com.shuqing.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: ShuqingLibrary
 * @ClassName BookServiceImpl
 * @description:
 * @author: Ats
 * @create: 2025-03-31 15:00
 * @Version 1.0
 */

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private RecordService recordService;
    @Override
    public PageResult selectNewBooks(Integer pageNum, Integer pageSize) {
        //分页方法
        PageHelper.startPage(pageNum,pageSize);
        //查询数据库，获取图书信息
        Page<Book> books = bookMapper.selectNewbooks();
        //返回带分页的结果数据到前端页面
        return new PageResult(books.getTotal(), books.getResult());
    }

    @Override
    public Book findBookById(Integer id) {
        return bookMapper.findById(id);
    }

    @Override
    public Integer borrowBook(Book book) {
        Book b = this.findBookById(book.getId());
        SimpleDateFormat sfm = new SimpleDateFormat("yyyy-MM-dd");
        String format = sfm.format(new Date());
        book.setBorrowTime(format);
        book.setStatus("1");
        book.setPrice(b.getPrice());
        book.setUploadTime(b.getUploadTime());
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult searchBooks(Book book, Integer pageNum, Integer pageSize) {
        //分页方法
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page = bookMapper.searchBooks(book);
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public Integer addBook(Book book) {
        //保证上架时间是当前添加时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        book.setUploadTime(df.format(new Date()));
        return bookMapper.addBook(book);
    }

    @Override
    public Integer editBook(Book book) {
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        book.setBorrower(user.getName());
        Page<Book> page;
        if ("ADMIN".equals(user.getRole())){
            page = bookMapper.selectBorrowed(book);
        } else {
            page = bookMapper.selectMyBorrowed(book);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public boolean returnBook(Integer id, User user) {
        //根据图书id获取图书信息
        Book book = this.findBookById(id);
        //判断借阅者和当前用户是否是一个人
        boolean isOneUser = user.getName().equals(book.getBorrower());
        //如果是，那么修改图书状态 为归还中
        if (isOneUser) {
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        //否则，不给修改
        return isOneUser;
    }

    @Override
    public boolean returnConfirm(Integer id) {
        Book book = this.findBookById(id);
        Record record = this.setRecord(book);
        book.setStatus("0");
        book.setBorrower("");
        book.setBorrowTime("");
        book.setReturnTime("");
        Integer count = bookMapper.editBook(book);
        if (count > 0) {
            Integer recordCount= recordService.addRecord(record);
            return recordCount > 0;
        }
        return false;
    }

    private Record setRecord(Book book) {
        Record record = new Record();
        record.setBookname(book.getName());
        record.setBorrower(book.getBorrower());
        record.setBookisbn(book.getIsbn());
        record.setBorrowTime(book.getBorrowTime());
        SimpleDateFormat sfm = new SimpleDateFormat("yyyy-MM-dd");
        String returnTimeNew = sfm.format(new Date());
        record.setRemandTime(returnTimeNew);
        return record;
    }
}
